//
//  SLStatusCell.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/25.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SnapKit

//自定义cell
class SLStatusCell: UITableViewCell {
    
    //MARK: - 属性
    //cell的bottomView的顶部约束
    var bottomViewBottomCons: Constraint?
    
    //微博类型
    var status: SLStatus? {
        didSet {
            //当控制器把模型传给cell的时候调用
            //将模型传递给SLStasusOriginalView
            originalView.status = status
            
            //判断是否有被转发的微博模型.有被转发的微博模型就显示被转发的微博view.否则,不加载
            bottomViewBottomCons?.uninstall()
            if let retweetStatus = status?.retweeted_status {
                //有转发的模型.就显示
                retweetView.hidden = false
                
                // 将这个被转发的模型赋值给被转发的view
                retweetView.retweetStatus = retweetStatus
                //添加约束
                bottomlView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                retweetView.hidden = true
                bottomlView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
//        originalView.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        perpaerUI()
    }

   private func perpaerUI() {
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomlView)
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
        //当3个参照都一样的时候可以简写
//        make.leading.equalTo(contentView)
//        make.top.equalTo(contentView)
//        make.trailing.equalTo(contentView)
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            //原创cell的底部根据originalView里面的内容
//            make.height.equalTo(80 + Int(arc4random_uniform(100)))
        }
    
        //被转发的view
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
    
        //底部视图
        bottomlView.snp_makeConstraints { (make) -> Void in
            self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
    
        //当使用autolayout来约束cell里面的控件.并且cell的高度想通过autolayout来自定确定.需要给contenview添加约束
        contentView.snp_makeConstraints { (make) -> Void in
           //contentView左上右参照cell
            make.leading.top.trailing.equalTo(self)
            //contentView底部参照bottomlView
            make.bottom.equalTo(bottomlView)
        }
        //cell的优化
        //异步绘制
        self.layer.drawsAsynchronously = true
    
        //栅格化/光栅化会离屏渲染
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
    
    
    //MARK: - 懒加载
    //原创微博的view
    private lazy var originalView: SLStasusOriginalView = SLStasusOriginalView()
    
    //底部视图
    private lazy var bottomlView: SLStatuBottomView = SLStatuBottomView()
   
    //被转发的view
    private lazy var retweetView: SLStatusRetweetView = SLStatusRetweetView()
}






